import {EditorMode, ElementType} from "@hufe921/canvas-editor";
import _ from "lodash";
import moment from "moment/moment.js";

let start = false;
const info = (d, ...argument) => {
    if (!window.editor) {
        return
    }
    window.editor.command.executeMode(EditorMode.EDIT);
    if (!start) {
        window.editor.command.executeSelectAll();
        start = true;
    }

    const a = window.editor.command.getText();
    if (a.main.length >= 10000) {
        clear();
        window.editor.command.executeMode(EditorMode.EDIT);
    }

    try {
        if (d.length > 10000) {
            d = d.substring(d.length - 10000, d.length - 1);
        }
    } catch (e) {
        d = d.substring(0, 9999);
    }

    d = moment().format('YYYY-MM-DD HH:mm:ss') + "=>" + d + "\n";

    window.editor.command.executeAppendElementList([{
        type: ElementType.TEXT, value: d
    }]);
    window.editor.command.executeMode(EditorMode.READONLY);
    // window.scrollTo(0,document.querySelector(".ce-page-container").scrollHeight);
    const element = document.querySelectorAll("canvas")[document.querySelectorAll("canvas").length - 1];
    element.scrollIntoView();
}

const clear = () => {
    window.editor.command.executeMode(EditorMode.EDIT);
    window.editor.command.executeSelectAll();
    window.editor.command.executeInsertElementList([{
        type: ElementType.TEXT, value: ""
    }]);
    window.editor.command.executeMode(EditorMode.READONLY);
}

export {info, clear}